type HandBrakeCallback = (
  type: 'progress' | 'close',
  data: number | boolean,
) => void

export async function convert(command: string, callback: HandBrakeCallback) {
  return new Promise((resolve, reject) => {
    NAS.exec(command, (type: string, data: string | number) => {
      if (type === 'progress') {
        const pencentMatch = (data as string).match(/(\d{1,3}\.\d{2}) %/)
        if (pencentMatch) {
          const pencent = pencentMatch[1]
          callback('progress', parseFloat(pencent))
        }
      } else if (type === 'close') {
        console.log(typeof data)
        if (data === 0) {
          resolve(true)
          callback('close', true)
        } else {
          reject(new Error('异常结束！'))
          callback('close', false)
        }
      } else if (type === 'error') {
        // if ((data as string).indexOf('has exited') > -1) {
        //   reject(new Error(data as string))
        // }
        console.log(data)
      }
    })
  })
}
